﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BloodApp.Common
{
    public static class ConfigurationHelper
    {
        /// <summary>
        /// 读取AppSettings.config文件中的键值对的值
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public static string GetConfigValue(string key)
        {
            System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            return configuration.AppSettings.Settings[key] != null ? configuration.AppSettings.Settings[key].Value : string.Empty;
        }
        public static bool SetConfigValue(string key, string newValue)
        {
            bool res = false;
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings[key].Value = newValue;
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                res = true;
            }
            catch
            {
                res = false;
            }
            return res;
        }
    }
}
